Region of interest mask


In [16]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = mpimg.imread('testimg.jpg')
xsize = img.shape[1]
ysize = img.shape[0]
region_select = np.copy(img)

Define a triangular region of interest, with the origin in the upper left


In [17]:
left_bottom = [45,750]
right_bottom = [1000,1000]
apex = [650, 400]

In [18]:
fit_left = np.polyfit((left_bottom[0], apex[0]), 
                      (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), 
                       (right_bottom[1], apex[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), 
                        (left_bottom[1], right_bottom[1]), 1)

In [19]:
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) & \
                    (YY > (XX*fit_right[0] + fit_right[1])) & \
                    (YY < (XX*fit_bottom[0] + fit_bottom[1]))

In [20]:
# red marker for region of interest
region_select[region_thresholds] = [255, 0, 0]
plt.imshow(region_select)
# sometimes plt.imshow doesn't work, so force show()
plt.show()



In [ ]: